package weaver.interfaces.jjy.duss.action;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.jjy.duss.hr.ZFM_HR_BZ_ZB;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-05-09
 * @Description: 年度编制推送SAP
 * @ModifyLog:
 **/

public class WeaHRNdbztsAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        baseBean.writeLog("请求ID" + requestid);
        baseBean.writeLog("调用WeaHRNdbztsAction");
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        rs.executeQuery("select id,ZEHR_ZTWB,ZEHR_FSRQ from " + tablename + " where requestid=?", requestid);
        baseBean.writeLog("select id,ZEHR_ZTWB,ZEHR_FSRQ from " + tablename + " where requestid=" + requestid);
        rs.next();
        //主表字段
        String mainid = Util.null2String(rs.getString("id"));//ID
        String ZEHR_ZTWB = Util.null2String(rs.getString("ZEHR_ZTWB"));//状态
        String ZEHR_FSRQ = Util.null2String(rs.getString("ZEHR_FSRQ").replaceAll("-", ""));//发送日期
        String BZLX = "1";
        //明细表1
        List<Map<String, String>> list = new ArrayList<>();
        String fileds = " ZEHR_NUMID_BZ,ZEHR_HDBZS_G,ZEHR_HDBZS_Z,ZEHR_HDBZS_M,ZEHR_HDBZS_J,ZEHR_SBRS_G,ZEHR_SBRS_Z,ZEHR_SBRS_M,ZEHR_SBRS_J ";
        rs.executeQuery("select "+fileds+" from " + tablename + "_dt1 where mainid=?", mainid);
        baseBean.writeLog("select "+fileds+" from " + tablename + "_dt1 where mainid=?", mainid);
        while (rs.next()) {
            String ZEHR_NUMID_BZ = Util.null2String(rs.getString("ZEHR_NUMID_BZ"));//唯一标识
            String ZEHR_HDBZS_G = Util.null2String(rs.getString("ZEHR_HDBZS_G"));//核定编制数（高层）
            String ZEHR_HDBZS_Z = Util.null2String(rs.getString("ZEHR_HDBZS_Z"));//核定编制数（中层）
            String ZEHR_HDBZS_M = Util.null2String(rs.getString("ZEHR_HDBZS_M"));//核定编制数（主管）
            String ZEHR_HDBZS_J = Util.null2String(rs.getString("ZEHR_HDBZS_J"));//核定编制数（基层）
            String ZEHR_SBRS_G = Util.null2String(rs.getString("ZEHR_SBRS_G"));//申报人数（高层）
            String ZEHR_SBRS_Z = Util.null2String(rs.getString("ZEHR_SBRS_Z"));//申报人数（中层）
            String ZEHR_SBRS_M = Util.null2String(rs.getString("ZEHR_SBRS_M"));//申报人数（主管）
            String ZEHR_SBRS_J = Util.null2String(rs.getString("ZEHR_SBRS_J"));//申报人数（基层）
            Map<String, String> map = new HashMap<>();
            map.put("NUMID" , ZEHR_NUMID_BZ);        //OA主键
            map.put("SXRQ" , ZEHR_FSRQ);         //生效日期
            map.put("BZLX" , BZLX);              //编制类型
            map.put("ZT" , ZEHR_ZTWB);           //状态文本
            map.put("HDRS_G" , ZEHR_HDBZS_G);     //核定人数（高层）
            map.put("HDRS_Z" , ZEHR_HDBZS_Z);     //核定人数（中层）
            map.put("HDRS_M" , ZEHR_HDBZS_M);     //核定人数（主管）
            map.put("HDRS_J" , ZEHR_HDBZS_J);     //核定人数（基层）
            map.put("SBRS_G" , ZEHR_SBRS_G);     //申报人数（高层）
            map.put("SBRS_Z" , ZEHR_SBRS_Z);     //申报人数（中层）
            map.put("SBRS_M" , ZEHR_SBRS_M);     //申报人数（主管）
            map.put("SBRS_J" , ZEHR_SBRS_J);     //申报人数（基层）
            list.add(map);
        }
        baseBean.writeLog("参数=" + list.toString());
        try {
            ZFM_HR_BZ_ZB bz_zb = new ZFM_HR_BZ_ZB();
            Map<String, String> resultMap = bz_zb.execute(list);
            String flag = Util.null2String(resultMap.get("flag"));
            String message = Util.null2String(resultMap.get("message"));
            baseBean.writeLog("SAP返回结果——flag:" + flag + ";message:" + message);
            if ("E".equalsIgnoreCase(flag)) {
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent(message);
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            baseBean.writeLog("异常参考:" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent(e.getMessage());
            return Action.FAILURE_AND_CONTINUE;
        }

    }
}
